#include "parsedatathread.h"

extern QVector<int> ZeroValue;
extern QVector<int> FullValue;
extern int CollectType;  // 1:连续采集;2:触发采集
extern int Frequency;
extern int TimeLength;
extern int VoltageLevel;
extern bool IsCollect;
extern QMutex MuteParse;

inline int ParseFreq(int Freq)
{
    switch (Freq) {
    case 500:
        return 2048;
    case 1000:
        return 1024;
    case 2000:
        return 512;
    case 4000:
        return 256;
    case 8000:
        return 128;
    case 16000:
        return 64;
    case 32000:
        return 32;
    default:
        return 1024;
    }
}

inline int ParseDisplayPoint(int CurFreq)
{
    switch (CurFreq) {
    case 512:
        return 2;
    case 1024:
        return 4;
    case 2048:
        return 8;
    default:
        break;
    }
}

ParseDataThread::ParseDataThread(QObject *parent) : QObject(parent)
{
    Init();
}

void ParseDataThread::Init()
{
    DataTimes = 0;
    DTInfo = GetCurrentDateTime();
    for(int i = 0; i < CHANNELCOUNT; i++) {
        DisplayData[i].clear();
        DisplayDataBuf[i].clear();
    }
}

// ParseData
float ParseDataThread::ParseData(int Ch, unsigned int SrcCode)
{
    switch (Ch) {
    case 1:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x400196) / (0x7e743a - 0x400196) * 10000;
        break;
    case 2:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x40018b) / (0x7e75c9 - 0x40018b) * 10000;
        break;
    case 3:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x400243) / (0x7e73df - 0x400243) * 10000;
        break;
    case 4:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x400156) / (0x7e7232 - 0x400156) * 10000;
        break;
    case 5:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x4000e0) / (0x7e7397 - 0x4000e0) * 10000;
        break;
    case 6:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x40016f) / (0x7e72b0 - 0x40016f) * 10000;
        break;
    case 7:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x400320) / (0x7e743a - 0x400320) * 10000;
        break;
    case 8:
        if(Ch <= ZeroValue.size())
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - ZeroValue.at(Ch - 1)) / (FullValue.at(Ch - 1) - ZeroValue.at(Ch - 1)) * 10000;
        else
            return ((float)((SrcCode & 0x8000000) ? (SrcCode | 0xFF000000) : (SrcCode & 0xFFFFFF)) - 0x400164) / (0x7e72c7 - 0x400164) * 10000;
        break;
    default:
        break;
    }
}

DateTimeInfo ParseDataThread::GetCurrentDateTime()
{
    DateTimeInfo DTInfo;
    QDateTime dt = QDateTime::currentDateTime();
    DTInfo.Year = dt.toString("yyyy");
    DTInfo.Month = dt.toString("MM");
    DTInfo.Day = dt.toString("dd");
    DTInfo.Hour = dt.toString("hh");
    DTInfo.Minute = dt.toString("mm");
    DTInfo.Second = dt.toString("ss");
    DTInfo.YearMonth = dt.toString("yyyy-MM");
    DTInfo.Date = dt.toString("yyyy-MM-dd");
    DTInfo.DateTime = dt.toString("yyyy-MM-dd_hh-mm-ss");
    return DTInfo;
}

// 接收数据事件
void ParseDataThread::ReceiveData(QByteArray Buf)
{
    if(Buf.isEmpty())
        return;
    unsigned char * data = (unsigned char*)(Buf.data());
    unsigned int SrcCode, Cnt = 0;
    // 解析并抽样数据
    int RealFreq = ParseFreq(Frequency);
    for(int i = 0; i < BYTESUM; i += RealFreq) { // RealFreq
        ++Cnt;
        unsigned int Ch = 0;
        // 保存数据到临时队列
        for(int j = i; j < i + 32; j += 4) {
            ++Ch;
            SrcCode = (data[j + 3] << 24) + (data[j + 2] << 16) + (data[j + 1] << 8) + data[j];
            float Value = ParseData(Ch, SrcCode);
            if(fabs(Value) > 10000)
                Value = 0;
            DisplayData[Ch - 1].push_back((double)Value); // 显示缓冲区
        }
    }
    QMutexLocker loker(&MuteParse);
    for(int k = 0; k < CHANNELCOUNT; k++) {
        DisplayDataBuf[k].clear();
        DisplayDataBuf[k] = DisplayData[k];
    }
    emit SendDisplayDate(DisplayDataBuf);
    for(int k = 0; k < CHANNELCOUNT; k++) {
        DisplayData[k].clear();
    }
}


